package entities;

import ejb.Verwaltung;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@ManagedBean
@RequestScoped
@Entity
public class Thema implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id @GeneratedValue(strategy= GenerationType.AUTO)
    private int id;
    private String thema;
    @OneToMany(cascade= CascadeType.ALL)
    private List<Nachricht> nachrichten;
    
    
    public Thema(){
        thema = "";
        nachrichten = new ArrayList();
    }
    
    public Thema(String thema){
        this.thema = thema;
        nachrichten = new ArrayList();
    }

    

    public String getThema() {
        return thema;
    }
    
    public void setThema(String thema) {
        this.thema = thema;
    }
    
    @Override
    public String toString() {
        return thema;
    } 
        
    
    
    public List<Nachricht> getNachrichten() {
        return nachrichten;
    }
    
    public int anzahlNachrichten(){
        return nachrichten.size();
    }

    public void addNachricht(Nachricht nachricht, Verwaltung v) {
        nachrichten.add(nachricht);
        v.persist(nachricht);
    }


    // Wird zum Vergleich von Objekten einer Liste verwendet
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Thema other = (Thema) obj;
        if ((this.thema == null) ? (other.thema != null) : !this.thema.equals(other.thema)) {
            return false;
        }
        return true;
    }
    
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + (this.thema != null ? this.thema.hashCode() : 0);
        return hash;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    
}
